import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import ProductsView from '../views/ProductsView.vue'
import ProductDetailView from '../views/ProductDetailView.vue'
import UserCenter from '../views/usercenter/UserCenter.vue';
import UserCustom from '../views/usercenter/UserCustom.vue';
import UserDetail from '../views/usercenter/UserDetail.vue';
import CallbackPage from '../views/CallbackPage.vue'
import RealNameAuth from '../views/usercenter/RealNameAuth.vue'// 新增实名认证组件
import GiteeCallback from "../views/GiteeCallback.vue";
import OrderManagement from '../views/usercenter/OrderManagement.vue' // 新增订单管理组件
import OrderDetail from '../views/usercenter/OrderDetail.vue' // 新增订单详情组件
import AiChat from '../views/AiChat.vue' // 新增订单管理组件

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
    },
    {
      path: '/solution',
      name: 'solution',
      component: () => import('../views/Solution.vue'),
    },
    
    {
      path: '/insuranceprocess',
      name: 'InsuranceProcess',
      component: () => import('../views/InsuranceProcess.vue'),
    },
 
    {
      path: '/ai_m',
      name: 'MedicalInsurance',
      component: () => import('../views/Ai_MedicalInsurance.vue'),
    },
    {
      path: '/ai_a',
      name: 'AccidentInsurance',
      component: () => import('../views/Ai_AccidentInsurance.vue'),
    },
    {
      path: '/ai_p',
      name: 'PropertyInsurance',
      component: () => import('../views/Ai_PropertyInsurance.vue'),
    },
    {
      path: '/websocket',
      name: 'websocket',
      component: () => import('../views/Websocket.vue'),
    },
    
    {
      path: '/map',
      name: 'map',
      component: () => import('../views/Map.vue'),
    },
    {
      path: '/about',
      name: 'about',
      component: () => import('../views/AboutView.vue'),
    },
    {
      path: '/list',
      name: 'list',
      component: () => import('../views/ListionsList.vue'),
    },
    {
      path: '/login',
      name: 'Login',
      component: () => import('../views/Login.vue'),
    },
      // 管理员登录
    {
      path: '/houlogin',
      name: 'Houlogin',
      component: () => import('../views/Houlogin.vue'),
    },
    {
      path: '/products',
      name: 'products',
      component: ProductsView
    },
    {
      path: '/products/:id',
      name: 'product-detail',
      component: ProductDetailView,
      props: true
    },
      // 后台管理路由
    {
      path: '/admincenter',
      name: 'AdminCenter',
      component: () => import('@/views/AdminCenter.vue'),
      meta: { requiresAdmin: true }
    },
    // 用户中心路由 - 更新
    {
      path: '/usercenter',
      component: UserCenter,
      meta: { requiresAuth: true },
      children: [
        {
          path: 'custom',
          component: UserCustom
        },
        {
          path: 'detail',
          component: UserDetail
        },
        // 新增实名认证路由
        {
          path: 'auth',
          component: RealNameAuth
        },
        // 添加订单管理路由
        {
          path: 'orders',
          component: OrderManagement
        },
        // 添加订单详情路由
        {
          path: 'order/detail/:id', // 使用订单ID作为参数
          name: 'order-detail',
          component: OrderDetail,
          props: true
        },
        {
          path: '', // 默认重定向到detail
          redirect: '/usercenter/detail'
        }
      ]
    },
    {
      path: '/dingtalk-callback',
      name: 'DingtalkCallback',
      component: CallbackPage,
      meta: { isPublic: true } // 标记为公开路由
    },
    {
      path: '/giteetalk-callback',
      name: 'GiteeCallback',
      component: GiteeCallback,
      meta: { isPublic: true } // 标记为公开路由
    },
    {
      path: '/aichat',
      name: 'AiChat',
      component: AiChat,
      meta: { isPublic: true } // 标记为公开路由
    },
    {
      path: '/payment-result',
      name: 'PaymentResult',
      component: () => import('../views/PaymentResult.vue'),
      meta: { isPublic: true }
    }
  ],
})

// // 路由守卫 - 检查登录状态
// router.beforeEach((to, from, next) => {
//   // 添加钉钉回调处理标记检查
//   const isProcessingDingtalk = localStorage.getItem('processingDingtalkLogin') === 'true'
//
//   // 如果是钉钉回调处理中，直接放行
//   if (isProcessingDingtalk) {
//     next()
//     return
//   }
//
//   const token = localStorage.getItem('token');
//   const publicRoutes = ['/login', '/dingtalk-callback', '/giteetalk-callback', '/aichat'];
//
//   if (to.meta.requiresAuth && !token) {
//     next('/login');
//   } else if (!publicRoutes.includes(to.path) && !token) {
//     next('/login');
//   } else {
//     next();
//   }
// });



    // 路由守卫 - 检查管理员权限（只允许管理员访问）所有页面跳转都会被守卫拦截和判断。
router.beforeEach((to, from, next) => {
  const role = localStorage.getItem('role')
  const token = localStorage.getItem('token')
  // 管理员中心权限
  if (to.meta.requiresAdmin && role !== 'admin') {
    next('/login')
  // 用户中心权限
  } else if (to.meta.requiresAuth && (!token || role !== 'user')) {
    next('/login')
  } else {
    next()
  }
})
export default router